<template>
  <basic-container>
    <avue-crud ref="crud"
               :option="option"
               :data="data"
               @expand-change="expandChanges"
               :page="page"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="expand"
                slot-scope="{row}">
        <avue-crud ref="crud"
                   :option="option1"
                   :data="row.childrens"
                   @on-load="onload(row)"
                   :row-style="rowStyle">
        </avue-crud>
      </template>
      <template slot="menu"
                slot-scope="scope">
        <el-button :size="scope.size"
                   :type="scope.type"
                   @click="rowDel(scope)">删除</el-button>
        <el-button :size="scope.size"
                   :type="scope.type"
                   @click="editTemplate(scope)">编辑</el-button>
        <el-button :size="scope.size"
                   :type="scope.type"
                   @click="viewTemplate(scope)">查看</el-button>
        <el-button :size="scope.size"
                   :type="scope.type"
                   @click="copyModule(scope)">复制模板</el-button>
      </template>
      <template slot="menuLeft"
                slot-scope="scope">
        <el-button size="mini"
                   type="primary"
                   @click="addTemplate(scope)">新增模板</el-button>
      </template>
    </avue-crud>
    <template-freight ref="templatefreight"
                      @success="addsuccess"
                      :viewData="viewData"
                      :whichType="whichType"
                      :editData="editData"></template-freight>
  </basic-container>
</template>

<script>
import templateFreight from '@/components/goods/seller/templateFreight';
import {
  remove,
  update,
  add,
  getTransportList,
  copyTransport,
  viewTemplate
} from "@/api/base/store/seller/transport";
import {
  mapGetters
} from "vuex";

export default {
  components: {
    templateFreight
  },
  data () {
    return {
      whichType:'addTransport',
      viewData: {},
      editData: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 20,
        pageSizes: [10, 20, 30, 40], //默认
      },
      params: {},
      form: {},
      query: {},
      loading: true,
      option: {
        addBtn: false,
        editBtn: false,
        delBtn: false,
        expand: true,
        refreshBtn: false,
        columnBtn: false,
        expandRowKeys: [],
        rowKey: 'id',
        defaultExpandAll: false,
        defaultSort:{
          prop: 'addTime',
          order: 'descending'
        },
		border:true,
		align:'center',
        column: [
          {
            label: '模板名称',
            prop: 'transName',
            span: 12,
            labelWidth: 120,
          },
          {
            label: '最后编辑时间',
            prop: 'addTime',
            span: 12,
            labelWidth: 120,
          }
        ]
      },
      option1: {
        border: true,
        header: false,
        addBtn: false,
        editBtn: false,
        refreshBtn: false,
        columnBtn: false,
        menu: false,
        size: 'mini',
        align: 'center',
        menuAlign: 'center',
        // height:'100',
        column: [{
          label: '配送方式',
          prop: 'transExpress',
          dicData: [{
            label: '快递',
            value: 1
          }, {
            label: '物流',
            value: 2
          },]
        }, {
          width: '300',
          label: '配送区域',
          prop: 'city_name'
        }, {
          label: '首重(kg)',
          prop: 'trans_weight'
        }, {
          label: '运费(￥)',
          prop: 'trans_fee'
        }, {
          label: '续重(kg)',
          prop: 'trans_add_weight'
        }, {
          label: '运费(￥)',
          prop: 'trans_add_fee'
        }]
      },
      data: []
    }
  },
  computed: {
    ...mapGetters(["permission", 'userInfo']),
    permissionList () {
      return {
        addBtn: this.vaildData(this.permission["/mall-cloud-base/transport/addItem"], false),
        viewBtn: this.vaildData(this.permission["/mall-cloud-base/transport/detail"], false),
        delBtn: this.vaildData(this.permission["/mall-cloud-base/transport/delete"], false),
        editBtn: this.vaildData(this.permission["/mall-cloud-base/transport/editItem"], false)
      };
    },
  },
  methods: {
    addsuccess () {
      this.onLoad(this.page);
    },
    addTemplate () {
      this.whichType = 'addTransport',
      this.$refs.templatefreight.open();
    },
    //复制模板
    copyModule (val) {
      copyTransport(val.row.id).then(res => {
        if (res.data.code == '200') {
          this.$message({
            type: "success",
            message: "复制成功!"
          });
          this.onLoad(this.page)
        } else {
          this.$message({
            type: "error",
            message: "复制失败！请重新复制"
          });
        }
      }, error => {

      });
    },
    onload(val){

    },
    rowStyle () {
      return {
        backgroundColor: '#f3f3f3',
        color: '#333333'
      }
    },
    expandChanges (row, expendList) {
        if(row.transType == 0){
            //按件数
            this.option1.column[2].label = "首件(件)"
            this.option1.column[4].label = "续件(件)"
        }
        else if(row.transType == 1){
            //按重量
            this.option1.column[2].label = "首重(kg)"
            this.option1.column[4].label = "续重(kg)"
        }
        else if(row.transType == 2){
            //按体积
            this.option1.column[2].label = "首体积(dm³)"
            this.option1.column[4].label = "续体积(dm³)"
        }
      // this.$message.success('展开回调')
    },
    rowSave (row, loading, done) {
      row.position = row.positionIds;
      row.deptName = row.$deptId;
      row.repassword = row.password;
      add(row).then(() => {
        loading();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        done();

      });
    },
    viewTemplate (row) {//查看
      let id = row.row.id;
      viewTemplate(id).then((res) => {
        console.log(res)
        if (res.data.code == 200) {
          let data = res.data.data;
          data.transExpressInfo = JSON.parse(data.transExpressInfo)
          if (typeof (data.transPostalRules) == 'object') {
            data.transPostalRules.transExpressInfo = JSON.parse(data.transPostalRules.transExpressInfo)
          }
          this.viewData = data;
          this.$refs.templatefreight.open();
        }
      });
    },
    editTemplate (row) {//编辑
      let id = row.row.id;
      viewTemplate(id).then((res) => {
        console.log(res)
        if (res.data.code == 200) {
          let data = res.data.data;
          data.transExpressInfo = JSON.parse(data.transExpressInfo)
          if (typeof (data.transPostalRules) == 'object') {
            data.transPostalRules.transExpressInfo = JSON.parse(data.transPostalRules.transExpressInfo)
          }
          this.editData = data;
          this.$nextTick(()=>{
	          this.whichType = 'editTransport'
          })
          this.$refs.templatefreight.open();
        }
      });
    },
    rowDel (row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          console.log(row)
          remove(row.row.id).then((res)=>{
          if(res.data.code==200){
          this.$message({
                      type: "success",
                      message: "操作成功!"
                    });
          }else {
            this.$message({
              type: "error",
              message: res.data.message
            });
          }
            this.onLoad(this.page);
         } )
        })

    },
    searchReset () {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange (params,done) {
      this.page.currentPage = 1
      this.query = params;
      this.onLoad(this.page, params,done);
    },
    currentChange (currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange (pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange () {
      this.onLoad(this.page);
    },
    onLoad (page, params = {},done) {
      this.loading = true;
      getTransportList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        if(done){
          done();
        }
        console.log(res.data)
        this.loading = false;
        const data = res.data;
        this.page.total = data.count;
        for (let item of data.data) {
          item.childrens = JSON.parse(item["transExpressInfo"])
          for (let obj of item.childrens) {
            obj.transExpress = item.transExpress
          }
        }
        this.data = data.data;
      });
    }
  }
};
</script>

<style>
</style>
